package www.bplead.com.printer;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyangzi on 2017/3/3.
 */

public class PrintService {
    private static final int printWidth = 608;//打印纸宽608px

    private static List<PrintService> printServiceList = new ArrayList<>();

    private int printerCurrentState = 0;
    private class PrinterState{
        public static final int STATE_NONE = 0;//监听连接的到来
        public static final int STATE_CONNECTED = 1;//已连接
        public static final int STATE_CONNECTING = 2;//正在连接
        public static final int STATE_CONNECTION_LOST = 3;//蓝牙已断开连接
        public static final int STATE_UNABLE_CONNECT = 4;//无法连接设备
    }

    private Context mContext;
    private BluetoothService bluetoothService;
    private String mac;

    public PrintService(Context mContext,String mac){
        this.mContext = mContext;
        this.mac = mac;
        if(printServiceList.size() > 0) {
            for(PrintService ps : printServiceList){
                ps.stopPrintSerice();
            }
            printServiceList.clear();
        }
        printServiceList.add(this);
    }

    private void connectPrinter(String mac) {
        BluetoothDevice bd = bluetoothService.getDevByMac(mac);
        bluetoothService.connect(bd);
    }

    /**
     * 打印文本
     * @param text
     */
    public void printTXT(String text){
        if(isAllowPrint()){
            bluetoothService.sendMessage(text+"\n", "GBK");
        }
    }

    /**
     * 打印图片
     * @param bm
     */
    public void printIMAGE(Bitmap bm){
        if(isAllowPrint()){
            byte[] sendData = null;
            PrintPic pg = new PrintPic();
            pg.initCanvas(printWidth);
            pg.initPaint();
            pg.canvas.drawBitmap(bm, 0, 0,null);
            if (pg.length < 0 + bm.getHeight())
                pg.length = (0 + bm.getHeight());
            sendData = pg.printDraw();
            bluetoothService.write(sendData);   //打印byte流数据
        }
    }

    /**
     * 开启打印服务
     */
    public void openPrintService(){
        //蓝牙服务未创建那么创建
        if(bluetoothService == null){
            bluetoothService = new BluetoothService(mContext,mHandler);
        }
        //未连接打印机那么连接
        if(printerCurrentState != PrinterState.STATE_CONNECTED){
            connectPrinter(mac);
        }
    }

    /**
     * 停止打印服务
     */
    public void stopPrintSerice(){
        if(bluetoothService != null){
            bluetoothService.stop();
        }
    }

    private boolean isAllowPrint(){
        if(printerCurrentState == PrinterState.STATE_CONNECTED){
            return true;
        }
        openPrintService();
        return false;
    }

    /**
     * 创建一个Handler实例，用于接收BluetoothService类返回回来的消息
     */
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case BluetoothService.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:   //已连接
                            Log.d("蓝牙调试", "已连接！");
                            printerCurrentState = 1;
                            Toast.makeText(mContext,"已连接！",Toast.LENGTH_SHORT).show();
                            break;
                        case BluetoothService.STATE_CONNECTING:  //正在连接
                            Log.d("蓝牙调试", "正在连接.....");
                            Toast.makeText(mContext,"正在连接设备...",Toast.LENGTH_SHORT).show();
                            printerCurrentState = 2;
                            break;
                        case BluetoothService.STATE_LISTEN:     //监听连接的到来
                        case BluetoothService.STATE_NONE:
                            printerCurrentState = 0;
                            Log.d("蓝牙调试","等待连接.....");
                            break;
                    }
                    break;
                case BluetoothService.MESSAGE_CONNECTION_LOST:    //蓝牙已断开连接
                    Log.d("蓝牙调试", "连接已断开.....");
                    Toast.makeText(mContext,"连接已断开",Toast.LENGTH_SHORT).show();
                    printerCurrentState = 3;
                    break;
                case BluetoothService.MESSAGE_UNABLE_CONNECT:     //无法连接设备
                    Log.d("蓝牙调试", "无法连接设备.....");
                    Toast.makeText(mContext,"无法连接设备",Toast.LENGTH_SHORT).show();
                    printerCurrentState = 4;
                    break;
            }
        }
    };
}
